package com.atguigu.guli.serviceedu.controller;


import com.atguigu.guli.serviceedu.entity.Video;
import com.atguigu.guli.serviceedu.entity.vo.VideoVo;
import com.atguigu.guli.serviceedu.service.VideoService;
import com.atguigu.servicebase.exception.Assert;
import com.atguigu.servicebase.result.R;
import com.atguigu.servicebase.result.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-02
 */

@Api(tags = "课时管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/edu/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @ApiOperation(value = "新增课时")
    @PostMapping("save-video-info")
    public R save(
            @ApiParam(name = "videoForm", value = "课时对象", required = true)
            @RequestBody VideoVo videoVo){
        videoService.saveVideoInfo(videoVo);
        return R.ok().message("保存成功");
    }

    @ApiOperation(value = "根据ID查询课时")
    @GetMapping("video-info/{id}")
    public R getVideInfoById(
            @ApiParam(name = "id", value = "课时ID", required = true, example = "1")
            @PathVariable String id){
        Video video = videoService.getById(id);
        Assert.notNull(video, ResponseEnum.GET_VIDEOINFO_ERROR);
        VideoVo videoVo = new VideoVo();
        BeanUtils.copyProperties(video, videoVo);
        return R.ok().data("item", videoVo);
    }

    @ApiOperation(value = "更新课时")
    @PutMapping("update-video-info")
    public R updateCourseInfoById(
            @ApiParam(name = "VideoInfoForm", value = "课时基本信息", required = true)
            @RequestBody VideoVo videoVo){
        Video video = new Video();
        BeanUtils.copyProperties(videoVo, video);
        boolean b = videoService.updateById(video);
        Assert.notFalse(b, ResponseEnum.UPDATE_VIDEOINFO_ERROR);
        return R.ok().message("更新成功");
    }

    @ApiOperation(value = "根据ID删除课时")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课时ID", required = true, example = "1")
            @PathVariable String id){
        boolean result = videoService.removeById(id);
        Assert.notFalse(result, ResponseEnum.DELETE_VIDEOINFO_ERROR);
        return R.ok().message("删除成功");
    }
}

